package com.azzcs.myrpc.client;

import com.azzcs.myrpc.request.RpcRequest;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/**
 * @Author: wzg
 * @Date: 2021/1/22 下午7:39
 */
public class ClientHandler<V> extends ChannelInboundHandlerAdapter implements Callable<V> {
    private ChannelHandlerContext context;
    private String result;
    private RpcRequest parameter;

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        context = ctx;
    }

    @Override
    public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) {
        result = msg.toString();
        notify();
    }

    @Override
    public synchronized V call() throws Exception {
        context.writeAndFlush(parameter);
        wait(1000);
        return (V)result;
    }

    public void setParameter(RpcRequest parameter) {
        this.parameter = parameter;
    }
}
